<template>
  <div class="payment-container">
    <div class="payment-header">
      <div class="amount-info">
        <span class="amount-label">支付金额</span>
        <span class="amount">¥{{ amount }}</span>
      </div>
      <div class="order-info">
        <span>订单号：{{ orderNo }}</span>
        <span class="countdown">支付剩余时间：{{ countdownText }}</span>
      </div>
    </div>

    <div class="payment-methods">
      <div class="method-title">选择支付方式</div>
      <div class="method-options">
        <el-radio v-model="payMethod" label="alipay" border>
          <i class="el-icon-money"></i>
          支付宝支付
        </el-radio>
      </div>
    </div>

    <div class="payment-content">
      <div v-if="payMethod === 'alipay'" class="alipay-container">
        <div class="loading">正在跳转到支付宝...</div>
      </div>
    </div>

    <div class="payment-footer">
      <el-button @click="$router.push('/payments')">返回订单列表</el-button>
      <el-button type="primary" @click="handlePayment">确认支付</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'UserPayment',
  data() {
    return {
      orderNo: '',
      amount: 0,
      payMethod: 'alipay',
      countdown: 900, // 15分钟倒计时
      timer: null
    }
  },
  computed: {
    countdownText() {
      const minutes = Math.floor(this.countdown / 60)
      const seconds = this.countdown % 60
      return `${minutes}:${seconds.toString().padStart(2, '0')}`
    }
  },
  created() {
    // 从路由参数获取订单信息
    const { orderNo, amount } = this.$route.query
    this.orderNo = orderNo
    this.amount = parseFloat(amount)
    this.startTimer()
    
    // 如果没有订单号，跳转回订单列表
    if (!this.orderNo) {
      this.$message.error('订单信息不完整')
      this.$router.push('/payments')
    }

    // 直接开始支付宝支付流程
    this.handlePayment()
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  },
  methods: {
    startTimer() {
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--
        } else {
          clearInterval(this.timer)
          this.$message.warning('支付超时，请重新下单')
          this.$router.push('/payments')
        }
      }, 1000)
    },
    async handlePayment() {
      try {
        const response = await this.request.post('/payment/alipay', {
          orderNo: this.orderNo,
          amount: this.amount,
          subject: `订单${this.orderNo}的支付`,
          body: `订单金额：${this.amount}元`
        });
        
        if (response.code === 1) {
          // 创建一个临时表单并提交到支付宝
          const div = document.createElement('div');
          div.innerHTML = response.data;
          document.body.appendChild(div);
          document.forms[0].submit();
          document.body.removeChild(div);
        } else {
          this.$message.error(response.msg || '创建支付订单失败');
        }
      } catch (error) {
        console.error('支付失败:', error);
        this.$message.error('创建支付订单失败，请重试');
      }
    }
  }
}
</script>

<style scoped>
.payment-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.payment-header {
  text-align: center;
  margin-bottom: 30px;
}

.amount-info {
  margin-bottom: 15px;
}

.amount-label {
  font-size: 16px;
  color: #666;
  margin-right: 10px;
}

.amount {
  font-size: 24px;
  color: #f56c6c;
  font-weight: bold;
}

.order-info {
  color: #666;
  font-size: 14px;
}

.countdown {
  margin-left: 20px;
  color: #409EFF;
}

.payment-methods {
  margin-bottom: 30px;
}

.method-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 15px;
}

.method-options {
  display: flex;
  justify-content: center;
}

.el-icon-money {
  margin-right: 8px;
  font-size: 20px;
  color: #409EFF;
}

.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  color: #909399;
}

.payment-footer {
  margin-top: 30px;
  text-align: center;
}

.el-radio {
  margin-right: 30px;
  padding: 10px 20px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.el-radio.is-bordered.is-checked {
  border-color: #409EFF;
}
</style> 